package com.game.modular.game.battle.panel.skillEffect;

import com.game.comment.util.BeanUtil;
import com.game.modular.game.battle.panel.skillEffect.base.PvpSkillEffectBaseService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 英雄战斗状态(pvpSkillEffect)表服务实现类
 *
 * @author cxy
 * @since 2024-08-22 11:35:32
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PvpSkillEffectServiceImpl extends PvpSkillEffectBaseService {

    /**
     * 查看是否存在技能效果
     */
    public Boolean hasNoNumberEffectSkill(Long battleId, Long playerHeroId, Long coreSkillEffectTypeId) {
        boolean back = true;
        for (int i = 0; i < 4; i++) {
            back = back && has(battleId, playerHeroId, coreSkillEffectTypeId, i);
        }
        return back;
    }

    /**
     * 武将
     * 未触发的技能效果(被动,指挥)
     */
    public List<PvpSkillEffect> getAllNoTriggerSkillEffect(Long battleId, Long playerHeroId) {
        List<PvpSkillEffect> list = list(battleId, playerHeroId);
        return list.stream().filter(pvpSkillEffect -> BeanUtil.selector()
                .bool(
                        new ArrayList<Object>() {{
                            add(2);
                            add(3);
                        }}.contains(pvpSkillEffect.getCoreSkillTyp())
                ).eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).eq(
                        0,
                        pvpSkillEffect.getStatus().getNumberOfTriggers()
                )
                .value()
        ).collect(Collectors.toList());
    }

    public List<PvpSkillEffect> getAllNoTriggerSkillEffect(Long battleId) {
        return list(battleId).stream().filter(pvpSkillEffect -> BeanUtil.selector()
                .bool(
                        new ArrayList<Object>() {{
                            add(2);
                            add(3);
                        }}.contains(pvpSkillEffect.getCoreSkillTyp())
                ).eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).eq(
                        0,
                        pvpSkillEffect.getStatus().getNumberOfTriggers()
                )
                .value()
        ).collect(Collectors.toList());
    }


    /**
     * 获取所有可以触发的技能效果列表
     *
     * @param battleId 战斗id
     * @param round    回合
     * @return List<PvpSkillEffect>
     */
    public List<PvpSkillEffect> allTriggeringList(
            Long battleId, Integer round
    ) {
        return list(battleId).stream().filter(pvpSkillEffect -> BeanUtil.selector().eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).bool(
                        pvpSkillEffect.obtainNumberOfEffectiveRounds().contains(round)
                ).value()
        ).collect(Collectors.toList());
    }

    /**
     * 获取武将可以触发的技能效果列表
     *
     * @param battleId     战斗id
     * @param playerHeroId 玩家英雄id
     * @param round        回合
     * @return List<PvpSkillEffect>
     */
    public List<PvpSkillEffect> heroTriggeringList(
            Long battleId, Long playerHeroId, Integer round
    ) {
        return allTriggeringList(battleId, round).stream().filter(pvpSkillEffect -> BeanUtil.selector().eq(
                        playerHeroId,
                        pvpSkillEffect.getTargetPlayerHeroId()
                ).value()
        ).collect(Collectors.toList());
    }


    /**
     * 获取所有
     * 满足参数条件的
     * 技能效果
     *
     * @param battleId     战斗id
     * @param round        当前回合
     * @param coreSkillTyp 技能来源类型
     * @return List<PvpSkillEffect>
     */
    public List<PvpSkillEffect> allTriggeringList(
            Long battleId, Integer coreSkillTyp, Integer round
    ) {
        return list(battleId).stream().filter(pvpSkillEffect -> BeanUtil.selector().eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).eq(
                        coreSkillTyp,
                        pvpSkillEffect.getCoreSkillTyp()
                ).bool(
                        pvpSkillEffect.obtainNumberOfEffectiveRounds().contains(round)
                ).value()
        ).collect(Collectors.toList());
    }

    public void remove(PvpSkillEffect pvpSkillEffect) {
        log.info(
                "技能效果({})移除成功,{}--->{}",
                pvpSkillEffect.getCoreSkillEffectTypeName(),
                pvpSkillEffect.getSourcePlayerHeroId(),
                pvpSkillEffect.getTargetPlayerHeroId()
        );
        remove(pvpSkillEffect.getBattleId(), pvpSkillEffect.getTargetPlayerHeroId(), pvpSkillEffect.getCoreSkillEffectId());
    }

    public void toUpdate(PvpSkillEffect pvpSkillEffect) {
        pvpSkillEffect.status.triggerFailReasons = new ArrayList<>();
        log.info(
                "技能效果({})更新成功,{}--->{}",
                pvpSkillEffect.getCoreSkillEffectTypeName(),
                pvpSkillEffect.getSourcePlayerHeroId(),
                pvpSkillEffect.getTargetPlayerHeroId()
        );
        pvpSkillEffect.status.numberOfTriggers +=1;

        Long battleId = pvpSkillEffect.getBattleId();
        String key1 = String.format(SKILL_EFFECT_PANEL_KEY1, battleId);
        redisUtils.hPut(key1, pvpSkillEffect.getCoreSkillEffectId().toString(), pvpSkillEffect, 20, TimeUnit.MINUTES);
    }

}
